<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        //dd(11);
        //api错误抛出异常

        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if($exception instanceof  ApiException ){

            return response()->json(
                [
                    'code'=>1,
                    'data'=> $exception->getMessage()
                ]
            )->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }
        if($exception instanceof  TeleException ){

            return response()->json(
                [
                    'result'=>'-9',
                    'data'=> $exception->getMessage()
                ]
            )->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }
        if(Request::is('api/*')) {
            $response = [];
            $error = $this->convertExceptionToResponse($exception);
            $response['code']=1;
            $response['data']['status'] = $error->getStatusCode();
            $response['data']['msg'] = 'something error';
            if (config('app.debug')) {
                $response['data']['msg'] = empty($exception->getMessage()) ? 'something error' : $exception->getMessage();
                if ($error->getStatusCode() >= 500) {
                    if (config('app.debug')) {
                      //  $response['data']['trace'] = $exception->getTraceAsString();
                       // $response['data']['code'] = $exception->getCode();
                    }
                }

                $response= response()->json($response);
                $response->header('Access-Control-Allow-Origin', '*');
                $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization,authorization, X-XSRF-TOKEN');
                $response->header('Access-Control-Expose-Headers', 'Authorization, authenticated');
                $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
                $response->header('Access-Control-Allow-Credentials', 'true');
                return $response;
            }
        }

        return parent::render($request, $exception);
    }}
